﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pong1
{
    ///*
       // First, declare a delegate type that will be used to fire events.
       //  This is the same delegate as System.EventHandler.
       //  This delegate serves as the abstract observer.
       //  It does not provide the implementation, but merely the contract.
       public delegate void EventHandler(object sender, EventArgs e);

       // Next, declare a published event.  This serves as the concrete subject.
       //  Note that the abstract subject is handled implicitly by the runtime.
       public class Button
       {
           // The EventHandler contract is part of the event declaration.
           public event EventHandler Clicked;

           // By convention, .NET events are fired from descendant classes by a virtual method,
           //  allowing descendant classes to handle the event invocation without subscribing
           //  to the event itself.
           protected virtual void OnClicked(EventArgs e)
           {
               if (Clicked != null)
                   Clicked(this, e); // implicitly calls all observers/subscribers
           }
       }

       // Then in an observing class, you are able to attach and detach from the events:
       public class Window
       {
           private Button okButton;

           public Window()
           {
               okButton = new Button();
               // This is an attach function.  Detaching is accomplished with -=.
               // Note that it is invalid to use the assignment operator - events are multicast
               //  and can have multiple observers.
               okButton.Clicked += new EventHandler(okButton_Clicked);
           }

           private void okButton_Clicked(object sender, EventArgs e)
           {
               // This method is called when Clicked(this, e) is called within the Button class
               //  unless it has been detached.
           }
       }
    
    
   //*/


    /*
    public class Stock
    {

        //declare a delegate for the event
        public delegate void AskPriceDelegate(object aPrice);
        //declare the event using the delegate
        public event AskPriceDelegate AskPriceChanged;

        //instance variable for ask price
        object _askPrice;

        //property for ask price
        public object AskPrice
        {

            set
            {
                //set the instance variable
                _askPrice = value;

                //fire the event
                AskPriceChanged(_askPrice);
            }

        }//AskPrice property

    }//Stock class

    //represents the user interface in the application
    public class StockDisplay
    {

        public void AskPriceChanged(object aPrice)
        {
            Console.Write("The new ask price is:" + aPrice + "\r\n");
        }

    }//StockDispslay class

    public class MainClass
    {

        public void Main()
        {

            //create new display and stock instances
            StockDisplay stockDisplay = new StockDisplay();
            Stock stock = new Stock();

            //create a new delegate instance and bind it
            //to the observer's askpricechanged method
            Stock.AskPriceDelegate aDelegate = new
               Stock.AskPriceDelegate(stockDisplay.AskPriceChanged);

            //add the delegate to the event
            stock.AskPriceChanged += aDelegate;

            //loop 100 times and modify the ask price
            for (int looper = 0; looper < 100; looper++)
            {
                stock.AskPrice = looper;
            }

            //remove the delegate from the event
            stock.AskPriceChanged -= aDelegate;

        }//Main
    }
        //*/


       

 }